package com.linxz.video.videoplayer;

import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.linxz.video.videoplayer.event.FullToSmallListener;
import com.linxz.video.videoplayer.event.SDKSlotListener;
import com.linxz.video.videoplayer.event.VideoPlayerListener;
import com.linxz.video.videoplayer.utils.Utils;
import com.linxz.video.videoplayer.view.CustomVideoView;
import com.linxz.video.videoplayer.view.VideoFullDialog;

/**
 * @author Linxz
 * 创建日期：2019年10月15日 18:59
 * version：v4.5.4
 * 描述：
 */
public class VideoAdSlot {

    private Context mContext;
    private CustomVideoView mVideoView;
    private ViewGroup mParentView;

    private String sourcePath;
    private SDKSlotListener mSDKSlotListener;

    public VideoAdSlot(String sourcePath, SDKSlotListener sdkSlotListener){
        this.sourcePath=sourcePath;
        this.mSDKSlotListener=sdkSlotListener;
        this.mParentView=mSDKSlotListener.getAdParent();
        mContext=mParentView.getContext();
        initVideoView();
    }

    private void initVideoView() {
        mVideoView = new CustomVideoView(mContext);
        if (sourcePath != null) {
            mVideoView.setDataSource(sourcePath);
            mVideoView.setVideoPlayerListener(mVideoPlayerListener);
        }
        RelativeLayout paddingView = new RelativeLayout(mContext);
        paddingView.setBackgroundColor(mContext.getResources().getColor(android.R.color.black));
        paddingView.setLayoutParams(mVideoView.getLayoutParams());
        mParentView.addView(paddingView);
        mParentView.addView(mVideoView);
    }

    private void backToSmallMode(int position) {
        if (mVideoView.getParent() == null) {
            mParentView.addView(mVideoView);
        }
        mVideoView.setTranslationY(0); //防止动画导致偏离父容器
        mVideoView.isShowFullBtn(true);
        mVideoView.mute(true);
        mVideoView.setVideoPlayerListener(mVideoPlayerListener);
        mVideoView.seekAndResume(position);
    }

    private void bigPlayComplete() {
        if (mVideoView.getParent() == null) {
            mParentView.addView(mVideoView);
        }
        mVideoView.setTranslationY(0); //防止动画导致偏离父容器
        mVideoView.isShowFullBtn(true);
        mVideoView.mute(true);
        mVideoView.setVideoPlayerListener(mVideoPlayerListener);
    }

    public void destroy() {
        mVideoView.onDestroy();
        mVideoView = null;
        mContext = null;
        sourcePath = null;
    }


    public void mute(boolean isMute){
        mVideoView.mute(isMute);
    }

    private VideoPlayerListener mVideoPlayerListener=new VideoPlayerListener(){
        @Override
        public void onPlayClick() {

        }

        @Override
        public void onVideoClick() {

        }

        @Override
        public void onVideoLoadSuccess() {
            if (mSDKSlotListener != null) {
                mSDKSlotListener.onVideoLoadSuccess();
            }
        }

        @Override
        public void onVideoLoadError() {
            if (mSDKSlotListener != null) {
                mSDKSlotListener.onVideoFailed();
            }
        }

        @Override
        public void onVideoLoadComplete() {
            if (mSDKSlotListener != null) {
                mSDKSlotListener.onVideoComplete();
            }
            mVideoView.setIsRealPause(true);
        }

        @Override
        public void onFullScreenClick() {
            //获取videoview在当前界面的属性
            Bundle bundle = Utils.getViewProperty(mParentView);
            mParentView.removeView(mVideoView);
            VideoFullDialog dialog = new VideoFullDialog(mContext, mVideoView, mVideoView.getCurrentPosition());
            dialog.setFullToSmallListener(new FullToSmallListener() {
                @Override
                public void getCurrentPlayPosition(int position) {
                    backToSmallMode(position);
                }

                @Override
                public void playComplete() {
                    bigPlayComplete();
                }
            });
            //为Dialog设置播放器数据Bundle对象
            dialog.setViewBundle(bundle);
            dialog.show();
        }

        @Override
        public void onSmallScreenClick() {
        }
    };
}
